<?php

declare(strict_types=1);

namespace App\CustHub\Listener;


use App\CustHub\Event\SmartAnswerTrigger;
use App\CustHub\Event\SmartQuestionTrigger;
use App\CustHub\Event\SopTagTrigger;
use App\CustHub\Service\ChSopService;
use App\CustHub\Service\SmartAnswerService;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use Psr\Container\ContainerInterface;


#[Listener]
class SmartAnswerListener implements ListenerInterface
{
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function listen(): array
    {
        // 返回一个该监听器要监听的事件数组，可以同时监听多个事件
        return [
            SmartAnswerTrigger::class,
        ];
    }

    /**
     * @param SmartAnswerTrigger $event
     */
    public function process(object $event): void
    {
        // 事件触发后该监听器要执行的代码写在这里，比如该示例下的发送用户注册成功短信等
        // 直接访问 $event 的 user 属性获得事件触发时传递的参数值
        $sopService = $this->container->get(ChSopService::class);
        //先触发sop退订
        $res = $sopService->autoReturn($event->params);
        if(!$res){
            //触发智能应答
            $SmartService = $this->container->get(SmartAnswerService::class);
            if(!$SmartService -> smartQuestion($event->params)){
                $SmartService -> smartAnswer($event->params);
            }
        }
    }

}